local assets = {
	Asset("ANIM", "anim/musha_ironbunnyhat.zip"),
	Asset("ANIM", "anim/musha_ironbunnyhat_goggles.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_ironbunnyhat.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_ironbunnyhat.tex")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end

	if not inst.EnergyOn then
		if inst.components.musha_equipment.currentlevel < 10 then
			inst.components.equippable.walkspeedmult = 1.05
		elseif inst.components.musha_equipment.currentlevel < 20 then
			inst.components.equippable.walkspeedmult = 1.08
		elseif inst.components.musha_equipment.currentlevel < 30 then
			inst.components.equippable.walkspeedmult = 1.12
		else
			inst.components.equippable.walkspeedmult = 1.15
		end
	else
		if inst.components.musha_equipment.currentlevel < 30 then
			inst.components.equippable.walkspeedmult =
				1.20 + math.floor((inst.components.musha_equipment.currentlevel - 1) / 5) * 0.05
		else
			inst.components.equippable.walkspeedmult = 1.50
		end
	end

	if inst.components.musha_equipment.currentlevel < 10 then
		inst.components.insulator.insulation = TUNING.INSULATION_SMALL
	elseif inst.components.musha_equipment.currentlevel < 20 then
		inst.components.insulator.insulation = TUNING.INSULATION_MED
	elseif inst.components.musha_equipment.currentlevel < 30 then
		inst.components.insulator.insulation = TUNING.INSULATION_MED_LARGE
	else
		inst.components.insulator.insulation = TUNING.INSULATION_LARGE
	end

	inst.components.armor:SetAbsorption(
		math.max(39 + inst.components.musha_equipment.currentlevel, inst.components.musha_equipment.currentlevel * 2 + 20) *
			0.01
	)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_HAT_BUNNYA ..
					" (LV%d)\n" .. STRINGS.MUSHA_ARMOR .. " (%d)\n" .. STRINGS.MUSHA_ITEM_WARMNCOOL .. "(%s)",
				inst.components.musha_equipment.currentlevel,
				math.max(19 + inst.components.musha_equipment.currentlevel, inst.components.musha_equipment.currentlevel * 2 - 12),
				(inst.components.musha_equipment.currentlevel < 10 and "Small") or
					(inst.components.musha_equipment.currentlevel < 20 and "MED") or
					(inst.components.musha_equipment.currentlevel < 20 and "MED2") or
					"Large"
			)
		)
	end
end

local function ChangeInsulation(inst)
	if TheWorld.state.issummer then
		inst.components.insulator:SetSummer()
	elseif not TheWorld.state.issummer then
		inst.components.insulator:SetWinter()
	end
end

local function Enable_Goggle(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.components.musha_equipment:IsBroken() and owner:HasTag("player") then
		if owner:GetSandstormLevel() >= TUNING.SANDSTORM_FULL_LEVEL and not owner.components.playervision:HasGoggleVision() then
			if not inst:HasTag("goggles") then
				inst:AddTag("goggles")
			end
		else
			inst:RemoveTag("goggles")
		end
	end
end

local function Energy_Switch(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.EnergyOn then
		if inst.components.musha_equipment:IsBroken() then
			return
		end

		inst.Goggle = inst:DoPeriodicTask(0.5, Enable_Goggle)
		owner.SoundEmitter:PlaySound("dontstarve_DLC001/common/moggles_on")
		inst.Light:Enable(true)
		inst.components.musha_equipment:StartConsuming()
		owner.AnimState:OverrideSymbol("swap_hat", "musha_bunnyhat_goggles", "swap_hat")

		inst.components.equippable.walkspeedmult =
			(inst.components.musha_equipment.currentlevel <= 20 and
			1.25 + 0.05 * math.floor(inst.components.musha_equipment.currentlevel / 5)) or
			(inst.components.musha_equipment.currentlevel <= 20 and 1.45) or
			1.50

		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_HAT_BUNNYA .. "(%s)\n" .. STRINGS.MUSHA_ITEM_LIGHT .. "\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(%d)",
				string.rep("I", (inst.components.equippable.walkspeedmult - 1.2) / 0.05),
				(inst.components.equippable.walkspeedmult - 1) * 100
			)
		)
	else
		owner.SoundEmitter:PlaySound("dontstarve_DLC001/common/moggles_off")
		inst.components.equippable.walkspeedmult = 1
		inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
		inst.Light:Enable(false)

		if inst.Goggle then
			inst.Goggle:Cancel()
			inst.Goggle = nil
		end
		inst.components.musha_equipment:StopConsuming()
		owner.AnimState:OverrideSymbol("swap_hat", "musha_ironbunnyhat", "swap_hat")
	end
	inst.EnergyOn = not inst.EnergyOn
	Updata(inst)
	inst.components.useableitem:StopUsingItem()
end

local function OnEquip(inst, owner)
	ChangeInsulation(inst)
	inst:WatchWorldState("season", ChangeInsulation)

	inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")

	owner.AnimState:OverrideSymbol("swap_hat", "musha_ironbunnyhat", "swap_hat")
	inst.Light:Enable(false)
	owner.AnimState:Hide("HAIR")
	owner.AnimState:Hide("HAIR_NOHAT")
	owner.AnimState:Show("HAT_HAIR")
	owner.AnimState:Show("HAT")

	Updata(inst)
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end

	inst.boost = false

	owner.AnimState:Hide("HAT")
	owner.AnimState:Hide("HAT_HAIR")
	owner.AnimState:Show("HAIR_NOHAT")
	owner.AnimState:Show("HAIR")
end

local function fn(sim)
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.entity:AddLight()
	inst.Light:SetFalloff(0.4)
	inst.Light:SetIntensity(.7)
	inst.Light:SetRadius(1.8)
	inst.Light:SetColour(155 / 255, 175 / 255, 195 / 255)
	inst.Light:Enable(false)

	inst.AnimState:SetBank("hat_mbunnya")
	inst.AnimState:SetBuild("musha_ironbunnyhat")

	inst.AnimState:PlayAnimation("idle")

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("hat", 750, 0.075)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.equippable.walkspeedmult = 1
				inst.components.talker:Say(
					STRINGS.MUSHA_HAT_BROKEN .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_ironbunnyhat.xml"

	inst:AddComponent("insulator")
	inst.EnergyOn = false
	inst.type = "ice"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end
return Prefab("musha_ironbunnyhat", fn, assets)
